package com.pajk.user.service.life;

import com.pajk.user.api.LifeService;
import com.pajk.user.exception.UserServiceException;
import com.pajk.user.model.LifeCustomInfo;
import com.pajk.user.service.common.http.HttpsChannel;
import org.apache.commons.lang.StringUtils;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.ClassPathResource;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * User: peide.zhang
 * Date: 14-5-23
 */
public class LifeServiceImpl implements LifeService {
    private static final Logger log  = LoggerFactory.getLogger(LifeServiceImpl.class);
    private static final String PHONE_NUMBER_XPATH              = "/REQUEST/phoneNumber";
    private static final String QUERY_RESPONSE_DATA_XPATH   = "/RESPONSE";

    @Resource
    HttpsChannel lifeHttpsChannel;

    private Document       queryDoc;
    
    private String              queryUrl;
    
    public void init() {
        SAXReader reader = new SAXReader();
        ClassPathResource queryXml = new ClassPathResource("META-INF/life/query.xml");
        try {
            queryDoc = reader.read(queryXml.getInputStream());
        } catch (DocumentException e) {
            log.error("lifeService xml format error!", e);
        } catch (IOException e) {
            log.error("lifeService error read xml file!", e);
        }
    }

    /**
     * 积分查询
     * @param phoneNumber 手机号
     * @return 寿险用户信息
     */
    public LifeCustomInfo queryCustomerInfoByPhoneNumber(String phoneNumber) {
        queryDoc.selectSingleNode(PHONE_NUMBER_XPATH).setText(phoneNumber);
        Document response = lifeHttpsChannel.call(queryUrl, queryDoc.asXML());
        Node returnDataNode = response.selectSingleNode(QUERY_RESPONSE_DATA_XPATH);
        if (returnDataNode == null) {
            throw new UserServiceException("reponse format error!response:" + response.asXML());
        }

        String errorCode = returnDataNode.valueOf("PA_RSLT_CODE");
        if ("000000".equals(errorCode)) {
            Node isCustomNode = returnDataNode.selectSingleNode("returnMap/IS_CUSTOM");
            Node isVIPNode = returnDataNode.selectSingleNode("returnMap/IS_CUSTOM");
            Node heigthNode = returnDataNode.selectSingleNode("returnMap/HEIGTH");
            Node weightNode = returnDataNode.selectSingleNode("returnMap/WEIGHT");
            String isLifeCustom=isCustomNode.getText();
            String isVIP=isVIPNode.getText();
            String heigh=heigthNode.getText();
            String weight=weightNode.getText();
            LifeCustomInfo lifeCustomInfo=new LifeCustomInfo();
            if(!StringUtils.isEmpty(isLifeCustom) && isLifeCustom.equalsIgnoreCase("y")){
                lifeCustomInfo.setLifeCustom(true);
            }
            if(!StringUtils.isEmpty(isVIP) && isVIP.equalsIgnoreCase("y")){
                lifeCustomInfo.setVIP(true);
            }
            if(!StringUtils.isEmpty(heigh)){
                lifeCustomInfo.setHeigh(Integer.valueOf(heigh));
            }
            if(!StringUtils.isEmpty(weight)){
                lifeCustomInfo.setWeight(Integer.valueOf(weight));
            }

            return lifeCustomInfo;
        } else {
                return new LifeCustomInfo();
        }
    }

    public void setQueryUrl(String queryUrl) {
        this.queryUrl = queryUrl;
    }
}
